package br.ucb.musicbay.conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.ucb.musicbay.album.Album;
import br.ucb.musicbay.musica.Musica;
import br.ucb.musicbay.musica.MusicaBO;

@FacesConverter(value = "musicaConversor", forClass = Album.class)
public class MusicaConversor implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		Musica musica = new Musica();
		if (value != null && value.trim().length() > 0) {
			Integer id = Integer.valueOf(value);
			try {
				MusicaBO musicaBO = new MusicaBO();
				musica = musicaBO.recoverById(id);
			} catch (Exception e) {
				throw new ConverterException("Nao Foi possivel encontrar a categoria de codigo" + value + "." + e.getMessage());
			}
		}
		return musica;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {

		if (object != null) {
			Musica musica = (Musica) object;
			return musica.getIdMusica().toString();
		}
		return null;
	}

}
